#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "stm32f10x.h"

#include "public_lib.h"
#include "serial.h"
#include "w25qxx.h"
#include "spi_flash_task.h"
#include "stm3210e_eval_spi_flash.h"

void start_spi_flash_task(void *args)
{
    //W25QXX_Init();
    sFLASH_Init();
    
    serial_writeline("ID : %08x", sFLASH_ReadID());
}

int spi_flash_task_run(void *args)
{
    
    
    return 0;
}

int spi_flash_task_manage(int argc, char **argv, void *args)
{
    int err_code = -1;

    if(argc >= 2)
    {
       
        if(strcasecmp(argv[0], "spi_flash")==0)
        {
            if(strcasecmp(argv[1], "read") == 0)
            {
                if(argc == 4)
                {
                    unsigned int addr = str_to_val(argv[2]);
                    unsigned int len = atoi(argv[3]);                    
                    if(addr < 0x1000000)
                    {
                        if((addr + len) < 0x1000000)
                        {
                            unsigned char *p = malloc(len);
                            if(p)
                            {
                               // sFLASH_ReadBuffer(p, addr, len);   
                                W25QXX_Read(p, addr, len);                                     
                                serial_write("spi_flash read :0x%02x", *p);
                                for(int i=1;i<len;i++)
                                {
                                    serial_write("-0x%02x", *(p + i));
                                }
                                free(p);
                                err_code = 0;
                            }
                            else
                            {
                                serial_writeline("mem malloc failed");
                            }
                        }
                        else
                        {
                            serial_writeline("addr+len should be less than 0x1000000");
                        }
                    }
                    else
                    {
                        serial_writeline("addr range : 0x0~0x1000000");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s addr len", argv[0], argv[1], argv[0], argv[1]);
                }                   
            }
            else if(strcasecmp(argv[1], "write")==0)
            {
                if(argc == 4)
                {
                    unsigned int addr = str_to_val(argv[2]);
                    unsigned int data = str_to_val(argv[3]);
                    W25QXX_Write((u8 *)&data, addr&0xFFFFFFFC, 4);
                    //sFLASH_WriteBuffer((u8 *)&data, addr&0xFFFFFFFC, 4);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s addr data", argv[0], argv[1], argv[0], argv[1]);
                }                
            }            
            else if(strcasecmp(argv[1], "erase") == 0)
            {                
                   if(argc == 3)
                {
                    unsigned int addr = str_to_val(argv[2]);                    
                    if(addr < 0x1000000)
                    {
                        //sFLASH_EraseSector(addr / 4096);
                        W25QXX_Erase_Sector(addr / 4096);            
                        err_code = 0;
                    }
                    else
                    {
                        serial_writeline("addr range : 0x0~0x1000000");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s addr[0x0] data", argv[0], argv[1], argv[0], argv[1]);
                }   
            }                        
        }        
        else{/* not for me, do nothing */}
    }
    else{/* not for me, do nothing */}

    return err_code;
}
